<template>
  <div class="history-page">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back-btn" @click="goBack">
        <i class="arrow-left">←</i>
      </div>
      <div class="title">群发历史</div>
    </div>

    <!-- 搜索框 -->
    <div class="search-box">
      <div class="search-input-wrapper">
        <input
          type="text"
          v-model="messageContent"
          placeholder="请输入群发消息关键字"
          class="search-input"
          @keyup.enter="handleSearch"
        >
        <button class="search-btn" @click="handleSearch">
          搜索
        </button>
      </div>
    </div>

    <!-- 历史记录列表 -->
    <div class="history-list" v-if="!loading">
      <div
        v-for="(item, index) in messageList"
        :key="index"
        class="history-item"
        @click="goToDetail(item.id)"
      >
        <div class="message-content">{{ item.messageContent }}</div>
        <div class="message-info">
          <span class="message-time">{{ item.creathTime }}</span>
          <span class="message-status" :class="{'status-success': item.status === 1, 'status-fail': item.status === 0}">
            {{ item.status === 1 ? '已发送' : '发送失败' }}
          </span>
        </div>
      </div>
      <div v-if="messageList.length === 0" class="empty-state">
        暂无群发记录
      </div>
    </div>
    <div v-else class="loading">
      <span v-if="retryCount > 0">正在重试({{ retryCount }}/{{ maxRetries }})...</span>
      <span v-else>加载中...</span>
    </div>
  </div>
</template>

<script>
import { selectMessage } from "@/api/message/message";

export default {
  data() {
    return {
      messageContent: null,
      messageList: [],
      loading: false,
      retryCount: 0,
      maxRetries: 3
    }
  },
  created() {
    this.getMessageList()
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    handleSearch() {
      this.getMessageList();
    },
    getMessageList() {
      selectMessage(this.messageContent).then(response => {
        console.log('获取群发记录:', response);
        if (response.code === 200) {
          this.messageList = response.data || [];
          this.retryCount = 0; // 成功后重置重试次数
        } else {
          this.$message.error(response.msg || '获取群发记录失败');
        }
      })
    },
    goToDetail(id) {
      this.$router.push({
        path: 'detail',
        query: { id: id }
      })
    },
  }
}
</script>

<style scoped lang="scss">
.history-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.nav-header {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid #eee;

  .back-btn {
    font-size: 20px;
    color: #333;
    margin-right: 16px;
  }

  .title {
    font-size: 18px;
    font-weight: 500;
  }
}

.search-box {
  padding: 12px 16px;
  background-color: #fff;

  .search-input-wrapper {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .search-input {
    flex: 1;
    height: 36px;
    background-color: #f5f5f5;
    border: none;
    border-radius: 18px;
    padding: 0 16px;
    font-size: 14px;

    &::placeholder {
      color: #999;
    }
  }

  .search-btn {
    height: 36px;
    padding: 0 20px;
    background-color: #409eff;
    color: white;
    border: none;
    border-radius: 18px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s;

    &:hover {
      background-color: #66b1ff;
    }

    &:active {
      background-color: #3a8ee6;
    }
  }
}

.history-list {
  padding: 16px;

  .history-item {
    background-color: #fff;
    padding: 16px;
    border-radius: 8px;
    margin-bottom: 12px;
    cursor: pointer;

    &:hover {
      opacity: 0.8;
    }

    .message-content {
      font-size: 14px;
      color: #333;
      line-height: 1.5;
      margin-bottom: 8px;
    }

    .message-info {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .message-time {
        font-size: 12px;
        color: #999;
      }

      .message-status {
        font-size: 12px;
        &.status-success {
          color: #52c41a;
        }
        &.status-fail {
          color: #ff4d4f;
        }
      }
    }
  }
}

.empty-state {
  text-align: center;
  color: #999;
  padding: 32px 0;
}

.loading {
  text-align: center;
  color: #999;
  padding: 32px 0;
}
</style>
